<?php 
	if(!isset($_SESSION)){
		session_start();
	}

	// Clear previous messages.
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');
	
	// Get values from the page.
	$userName = $_POST['userName'];	
	$userFullName = $_POST['userFullName'];
	$userTypeId = $_POST['userTypeId'];
	$userAddress = $_POST['userAddress'];
	$userTelephone = $_POST['userTelephone'];
	
	$userPassword = MD5('123456'); // Encrypt the default password with MD5 algorithm.
	
	// Check for mandatory fields.
	if($userName == "" ||  $userFullName == "" || $userTypeId < 1){
		$_SESSION['error'] = "Please complete the mandatory fields";
	}else{
		include("../dbconnection.php");
		// Check whether the user is already there.
		if($result = mysql_query("SELECT * FROM users WHERE userName = '$userName'")){
			if(mysql_num_rows($result) > 0){
				$_SESSION['error'] = "User Name : ".$userName." is not available";
			}else{
				
				//$userPassword = MD5($userPassword);
				$today = date('Y-m-d');
				if(mysql_query("INSERT INTO users (userName, userPassword, userFullName, userTypeId, userAddress, userTelephone, status, loginAttempts, userRegistrationDate) VALUES ('$userName', '$userPassword', '$userFullName', '$userTypeId', '$userAddress', '$userTelephone', 'ACTIVE', '0', '$today')")){
					$_SESSION['success'] = "User (".$userName.") registered successfully!";
				}else{
					$_SESSION['error'] = "Error occured!".mysql_error();
				}
			}
		}else{
			$_SESSION['error'] = "Error Occured.".mysql_error();		
		}
	}
	$_SESSION['page'] = "addUsers.php";
	header('Location: ../../');	
?>